package com.youxuit.youxu.kecheng.threadPool;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

public class ThreadPoolTest01 {
    public static void main(String[] args) throws InterruptedException {

        threadTest();
        threadPoolTest();
    }
    private static void threadPoolTest() throws InterruptedException{
        long start = System.currentTimeMillis();
        final Random random = new Random();
        final List<Integer> list = new ArrayList<Integer>();
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 100000; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    list.add(random.nextInt());
                }
            });
        }

        executorService.shutdown();
        boolean isStop = executorService.awaitTermination(1, TimeUnit.HOURS);
        System.out.println("is pool finished: " + isStop);
        System.out.println("threadPollTest耗时：" + (System.currentTimeMillis() - start));
        System.out.println("数据量为：" + list.size());
    }

    private static void threadTest() throws InterruptedException{
        long start = System.currentTimeMillis();
        final Random random = new Random();
        final List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < 100000; i++) {
            Thread thread = new Thread(){

                @Override
                public void run() {
                    list.add(random.nextInt());
                }
            };
            thread.start();
            thread.join();
        }

        System.out.println("threadTest耗时：" + (System.currentTimeMillis() - start));
        System.out.println("数据量为：" + list.size());
    }
}
